// pages/charge/prescriptionQuery.js
const api = require("../../utils/api")
const util = require("../../utils/util")
const cache = require("../../utils/cache")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    main: {},
    charge: '0',
    disabled: false
  },
  randomString(len) {
    len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var maxPos = $chars.length;
    var pwd = '';
    for (let i = 0; i < len; i++) {
      pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
  },
  hideModal(e) {
    this.setData({
      show: false
    })
  },
  handlerCharge() {
    this.setData({
      disabled: true
    })
    let arr = cache.getValue('patient')
    let cur = {}
    arr.forEach(m => {
      if (m.crtFlag == 'Y') {
        cur = m
      }
    })
    if (cur.ioNo == undefined || cur.ioNo == null || cur.ioNo == '') {
      this.setData({
        disabled: false
      })
      return
    }
    let prescriptionIds = ''
    let fee = 0
    this.data.main.prescriptionDtoList.forEach(m => {
      prescriptionIds += m.prescriptionId + ','
      fee += m.chargeFee
    })
    if (prescriptionIds == '') {
      this.setData({
        disabled: false
      })
      return
    }
    let params = {
      prescriptionIds: prescriptionIds.substr(0, prescriptionIds.length - 1),
      serialNumber: this.randomString(12),
      chargeFee: fee,
      chargeTime: util.formatTime(new Date()),
      target: '100013',
      ioNo: cur.ioNo
    }
    //console.log(params)
    let that = this
    api.charge(params).then(res => {
      that.setData({
        message: res.windowGuide + res.guide
      })
    }).catch(err => {
      that.setData({
        message: '支付失败!请联系门诊办处理'
      })
    })
    that.setData({
      disabled: false
    })
  },
  handlePrescriptionDetail(e) {
    let prescription = this.data.main.prescriptionDtoList.find(m => m.prescriptionId == e.currentTarget.dataset.id)
    if (prescription == undefined || prescription == null || prescription == '') {
      return;
    }
    wx.navigateTo({
      url: '/pages/charge/prescriptionDetailQuery',
      success: function (res) {
        res.eventChannel.emit('prescription', prescription)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.charge != undefined && options.charge != null && options.charge != '') {
      this.data.charge = options.charge
    }
    let that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('main', function (data) {
      that.data.main = data
      that.setData({
        main: that.data.main,
        charge: that.data.charge
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})